<?php
/**
 * Return a JSON string with massages taht init the rail log.
 *
 * PHP versions 5
 *
 * Copyright (C)
 * This program is free software. You can redistribute it and/or modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation. Either version 2 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY. Without even the implied warranty
 * of ERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with this program. Ff not, write to the Free Software
 * Fundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA
 *
 * Copyright (C) 2006-2007
 *
 * @author Bastian Schröder <bs@chemanet.de>
 * @version 2008-04-10
 * @package railadmin
 */

function ajax_call( $ajaxBridge ) {
	global $mysqli, $rail;
	$messages = array();

	// count rail objects
	$num_obj = 0;
	$data = $mysqli->query( "SELECT id FROM objects" );
	while( $data_row = $data->fetch_assoc() ) $num_obj ++;

	// get databases size
	$data = $mysqli->query("SHOW TABLE STATUS FROM " . $_SESSION["railadmin_MYSQL_db"] . " LIKE 'objects%'");
	$size = 0;
	while( $table = $data->fetch_assoc() ) $size += $table['Data_length'];

	$messages[] = array( 'type' => 'info',
						 'text' => 'MySQL Datenbank: ' . $_SESSION['railadmin_MYSQL_db'] .
								   '<br />RAIL Version: ' . $rail->get_root()->get_attribute('RAIL_VERSION') .
								   "<br />Anzahl an RAIL Objekten: $num_obj" .
								   '<br />Größe der MySQL Datenbank: ' . format_size( $size ) .
								   '<br />Aktueller Benutzer: ' . $rail->get_user()->get_name()
	);

	// warn if database version unique current PHPrail version
	if( $rail->get_root()->get_attribute('RAIL_VERSION') != PHPrail_VERSION )
		$messages[] = array( 'type' => 'warn',
							 'text' => 'Die Version des RAIL stimmt nicht mit der des installierten PHPrail Interfaces überein.'
		);

	// rail_Version: fix
	$version = $rail->get_root()->get_attribute('RAIL_VERSION');
	if( strpos($version, '-') )
		$version = substr( $version, 0, strpos($version, '-') );

	$ajaxBridge->append(array(
		'messages' => $messages,
		'rail_version' => $version
	));
}


/* ------------------------------------ functions ------------------------------------- */
// format size
function format_size ( $pSize ) {
	if( $pSize >= 1073741824 )
		return round(($pSize / 1073741824), 2) . "GB";
	elseif( $pSize >= 1048576 )
		return round(($pSize / 1048576), 2) . "MB";
	elseif( $pSize >= 1024 )
		return round(($pSize / 1024), 2) . " KB";
	else
		return $pSize . " Byte";
}

?>